package com.ysj.aipicturebe.config.MqConfig;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 **/
@Configuration // 标记为配置类，Spring会自动加载
public class RabbitMQConfig {

    public static final String THUMB_EXCHANGE = "thumb_exchange_topic"; // 交换机名称
    public static final String doThumb = "add"; // 路由键
    public static final String THUMB_QUEUE = "thumb_queue_topic"; // 队列名称

    @Bean // 定义队列Bean
    Queue testQueue() {
        return new Queue(THUMB_QUEUE, true, false, false); // 创建持久化队列
    }

    @Bean // 定义交换机Bean
    DirectExchange testExchange() {
        return new DirectExchange(THUMB_EXCHANGE, true, false); // 创建持久化直连交换机
    }

    @Bean // 定义绑定关系Bean
    Binding testBinding() {
        return BindingBuilder.bind(testQueue()).to(testExchange()).with(doThumb); // 队列与交换机通过路由键绑定
    }

    @Bean // 定义批量消费容器工厂Bean
    SimpleRabbitListenerContainerFactory consumerBatchContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); // 创建工厂实例
        factory.setConnectionFactory(cachingConnectionFactory()); // 设置连接工厂
        factory.setBatchListener(true); // 启用批量监听
        factory.setBatchSize(1000); // 每批最多消费1000条消息
        factory.setReceiveTimeout(1000L);  // 最多等待1秒，未满也触发消费
        factory.setConsumerBatchEnabled(true); // 启用消费者批量处理
        factory.setMessageConverter(messageConverter()); // 设置消息转换器
        factory.setConcurrentConsumers(3); // 设置并发消费者数量为3
        return factory; // 返回工厂实例
    }

    @Bean // 定义RabbitMQ连接工厂Bean
    CachingConnectionFactory cachingConnectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory(); // 创建连接工厂实例
        factory.setHost("1.116.239.110"); // 设置主机
        factory.setPort(5672); // 设置端口
        factory.setUsername("rabbitmq"); // 设置用户名
        factory.setPassword("rabbitmq"); // 设置密码
        factory.setVirtualHost("/"); // 设置虚拟主机
        return factory; // 返回连接工厂实例
    }

    @Bean // 定义消息转换器Bean
    public MessageConverter messageConverter() {
        ObjectMapper objectMapper = new ObjectMapper(); // 创建ObjectMapper
        objectMapper.registerModule(new JavaTimeModule()); // 注册时间模块，支持LocalDateTime等
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(objectMapper); // 创建JSON消息转换器
        converter.setCreateMessageIds(true); // 自动创建消息ID
        return converter; // 返回消息转换器
    }

    @Bean // 定义RabbitTemplate Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory); // 创建RabbitTemplate实例
        template.setMessageConverter(messageConverter()); // 设置消息转换器
        return template; // 返回RabbitTemplate实例
    }
}